package com.whq.thrift.api.web.demo01.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.whq.thrift.api.common.constants.CommonConfigConstants;
import com.whq.thrift.api.common.thriftclient.ThriftApiJsonClientHelper;
import com.whq.thrift.api.web.demo01.model.ResponseUserList;
import com.whq.thrift.api.web.demo01.model.UserInfo;

/**
 * 首页
 * @author whq-mac
 *
 */

@Controller
@Scope("prototype")
public class IndexController {
	private static final Logger LOGGER = LoggerFactory.getLogger(IndexController.class);
	
	@Autowired
	private ThriftApiJsonClientHelper thriftApiJsonClientHelper;
	
	
	@RequestMapping(value="index/index")
	public String index(Model model,
	        HttpServletRequest request) {
		
		Map<String, String> parameters = new HashMap<String, String>(0);
		parameters.put(CommonConfigConstants.THRIFT_PARAM_METHOD, "bsAccount.Test.syncList");
		
		Map<String, String> ctx = new HashMap<String, String>(0);
		
		String res = thriftApiJsonClientHelper.syncInvokeThriftService("bs-demo01", parameters, ctx, 10000, "web-demo01/index/index");
		
		ResponseUserList retList = null; 
		try {
			retList = JSON.parseObject(res, ResponseUserList.class);
		} catch (Exception e) {
			LOGGER.error("parse [" + res + "] to ResponseBase exception: ", e);
		}
		
		if ( (retList != null) && (retList.getData() != null) ) {
			model.addAttribute("resultList", retList.getData());
		} else {
			model.addAttribute("resultList", new ArrayList<UserInfo>(0));
		}
		
		return "index/index";
	}
}

